import { navigateTo, setPageLayout } from "nuxt/app";
let loginPanelLayoutArr = ["/login", "/register", "/recover", "/login/old"];

const setLayoutFunc = (to) => {
  let layout_index = -1;
  if (loginPanelLayoutArr.includes(to.path)) {
    if (isMobile()) {
      layout_index = 1;
      setPageLayout("mobile-login-panel");
    } else {
      layout_index = 2;
      setPageLayout("login-panel");
    }
  } else if (to.path.includes("profile")) {
    layout_index = 6;
    if (to.path.includes("new_")) setPageLayout("profile");
    else setPageLayout("layout");
  } else {
    if (to.meta.page_404) {
      setPageLayout("default");
      layout_index = 3;
    } else {
      if (to.path == "/pdf" || to.path == "/pay") {
        layout_index = 4;
        setPageLayout("default");
      } else {
        layout_index = 5;
        setPageLayout("layout");
      }
    }
  }

};

export default defineNuxtRouteMiddleware((to, from) => {
  setLayoutFunc(to);
  window?.addEventListener("resize", () => {
    setLayoutFunc(to);
  });
});
